package com.example.yichuangyun.mvvm.ui.activity.order;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityOrderDetailsBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.call.TabModel;
import com.example.yichuangyun.model.order.OrderListDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.CompanyDetailsActivity;
import com.example.yichuangyun.mvvm.ui.activity.order.view.OrderDetailsViewModel;
import com.example.yichuangyun.mvvm.ui.fragment.OrderBusinessFragment;
import com.example.yichuangyun.mvvm.ui.fragment.OrderFollowFragment;
import com.example.yichuangyun.mvvm.ui.fragment.OrderMoreFragment;
import com.example.yichuangyun.mvvm.ui.fragment.OrderServiceFragment;
import com.example.yichuangyun.ui.adapter.ViewPagerAdapter;
import com.example.yichuangyun.utils.JumpIntent;
import com.example.yichuangyun.utils.RequestUtils;
import com.example.yichuangyun.utils.ToastHelper;
import com.example.yichuangyun.utils.UMengUtil;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单详情
 */
public class OrderDetailsActivity extends BaseActivity<ActivityOrderDetailsBinding, OrderDetailsViewModel> {

    private final ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] titles;
    private final List<Fragment> fragments = new ArrayList<>();
    private String id;
    private OrderListDataModel.OrderRecordsModel orderRecordsModel;
    private String mTelNumber, businessId, clientName;
    private MyDialog myDialog;
    /**
     * 0电话拨打、1款项确认
     */
    private int flag;
    /**
     * 1搜索过来
     */
    private int from;

    @Override
    protected OrderDetailsViewModel bindModel() {
        return getViewModel(this, OrderDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar();
        UMengUtil.setCountEventId(Constant.ORDER_DETAIL);
        titles = new String[]{getString(R.string.service_text), getString(R.string.business), getString(R.string.follow), getString(R.string.more)};
        for (String title : titles) {
            mTabEntities.add(new TabModel(title));
        }
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            from = bundle.getInt(Constant.fromKey, 0);
            id = bundle.getString(Constant.idKey);
            viewModel.getOrderDetail(id);
        }
        initDialog();
    }

    @Override
    protected void initClick() {
        viewModel.orderRecordsModelMutableLiveData.observe(this, orderRecordsModel -> {
            this.orderRecordsModel = orderRecordsModel;
            binding.llPaymentConfirm.setEnabled(orderRecordsModel.getMoneyConfirmStatus() != 1 && orderRecordsModel.getOrderStatus() != -1 && orderRecordsModel.getOrderStatus() != 3);
            binding.tvPaymentConfirmText.setTextColor(context.getColor(orderRecordsModel.getMoneyConfirmStatus() != 1 && orderRecordsModel.getOrderStatus() != -1 && orderRecordsModel.getOrderStatus() != 3 ? R.color.color_426FFF : R.color.color_6A84A2));
            binding.tvCompanyName.setEnabled(orderRecordsModel.getEnterConfirmState() == 1);
            binding.tvCompanyName.setTextColor(context.getColor(orderRecordsModel.getEnterConfirmState() == 1 ? R.color.color_426FFF : R.color.color_3B3A40));
            binding.setOrderModel(orderRecordsModel);
            initFragment();
        });

        viewModel.onDelayClick(binding.tvCompanyName, o -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.creditNoKey, orderRecordsModel.getCreditNo());
            JumpIntent.jump(this, CompanyDetailsActivity.class, bundle);
        });

        viewModel.confirmSuccessData.observe(this, s -> {
            dismissLoadingDialog();
            ToastHelper.show(getString(R.string.confirm_success));
            binding.llPaymentConfirm.setEnabled(false);
            binding.tvPaymentConfirmText.setTextColor(context.getColor(R.color.color_6A84A2));
            EventBus.getDefault().post(new EventBusModel(from == 1 ? Constant.refreshSearchOrderList : Constant.refreshOrderList));
        });

        viewModel.mErrorData.observe(this, s -> {
            dismissLoadingDialog();
            ToastHelper.show(s);
        });

        viewModel.onDelayClick(binding.tvPhone, o -> {
            flag = 0;
            if (orderRecordsModel != null) {
                mTelNumber = orderRecordsModel.getPhone();
                businessId = orderRecordsModel.getBusinessId();
                clientName = orderRecordsModel.getUsername();
                myDialog.setIconVisibility(View.GONE);
                RequestUtils.checkCallNum(context, mTelNumber, myDialog, 0, true);
            }
        });

        viewModel.onDelayClick(binding.llCall, o -> {
            flag = 0;
            if (orderRecordsModel != null) {
                mTelNumber = orderRecordsModel.getPhone();
                businessId = orderRecordsModel.getBusinessId();
                clientName = orderRecordsModel.getUsername();
                myDialog.setIconVisibility(View.GONE);
                RequestUtils.checkCallNum(context, mTelNumber, myDialog, 0, false);
            }
        });

        viewModel.onDelayClick(binding.llAddFollow, o -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.orderIdKey, orderRecordsModel.getId());
            bundle.putString(Constant.orderNoKey, orderRecordsModel.getOrderNo());
            JumpIntent.jump(this, OrderAddFollowActivity.class, bundle);
        });

        viewModel.onDelayClick(binding.llPaymentConfirm, o -> {
            flag = 1;
            if (orderRecordsModel != null) {
                double price = orderRecordsModel.getPrice();
                double payedPrice = orderRecordsModel.getPayedPrice();
                if (payedPrice < price) {
                    ToastHelper.show(getString(R.string.unable_payment_confirm));
                } else {
                    myDialog.setText(context.getString(R.string.order_finish_pay_confirm), context.getString(R.string.order_payment_confirm_tips), context.getString(R.string.cancel), context.getString(R.string.confirm));
                    myDialog.setIconVisibility(View.VISIBLE);
                    myDialog.showDialog();
                }
            }
        });

        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    private void initDialog() {
        myDialog = new MyDialog(this) {
            @Override
            public void leftClick() {
                if (flag == 0) {
                    RequestUtils.callNum(context, mTelNumber);
                    Constant.pushBusinessId = businessId;
                }
            }

            @Override
            public void rightClick() {
                if (flag == 0) {
                    RequestUtils.callbackOutbound(context, mTelNumber, businessId, clientName);
                } else {
                    showLoadingDialog();
                    viewModel.moneyConfirm(id);
                }
            }
        };
    }

    private void initFragment() {
        fragments.add(OrderServiceFragment.newInstance(id));
        fragments.add(OrderBusinessFragment.newInstance(orderRecordsModel.getBusinessId()));
        fragments.add(OrderFollowFragment.newInstance(id, orderRecordsModel.getPhone()));
        fragments.add(OrderMoreFragment.newInstance(id, orderRecordsModel));
        binding.viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), 0, titles, fragments));
        binding.tabLayout.setTabData(mTabEntities);
    }
}